package com.example;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/a")
public class CheckUsernameServlet extends HttpServlet {
    UserDao userDao = new UserDao();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        boolean exist = false;

        String username = req.getParameter("username");

        User userInDb = userDao.getUserByName(username);

        if (userInDb!=null){
            exist = true;
        }

        resp.setContentType("application/json");
        resp.setCharacterEncoding("utf-8");

        PrintWriter  out = resp.getWriter();
        out.print("{\"exists\": " + exist + "}");
        out.flush();


    }
}
